Everything Totally Explained


Ask & we'll explain, totally!
Ruby (programming language)
Totally Explained


  NEW! All the latest news in the worlds of computer gaming, entertainment, the environment,  
finance, health, politics, science, stocks & shares, technology and much, much, more.  


View this entry using RSS

Everything about Ruby Programming Language totally explained

| typing = dynamic ("duck") | implementations = Ruby MRI, YARV, JRuby, Rubinius | influenced_by = Smalltalk, Perl, Lisp, Scheme, Python, CLU, Eiffel, Ada, Dylan | influenced = Groovy | operating_system = Cross-platform | license = Ruby License
GNU General Public License | website = www.ruby-lang.org }} Ruby is a dynamic, reflective, general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was initially developed and designed by Yukihiro "Matz" Matsumoto.
   Ruby supports multiple programming paradigms, including functional, object oriented, imperative and reflection. It also has a dynamic type system and automatic memory management; it's therefore similar in varying respects to Python, Perl, Lisp, Dylan, and CLU.
   In its current, official implementation, written in C, Ruby is a single-pass interpreted language. There is currently no specification of the Ruby language, so the original implementation is considered to be the de facto reference. As of 2008, there are a number of alternative implementations of the Ruby language, including Rubinius, JRuby, YARV, and IronRuby, each of which takes a different approach, with JRuby and IronRuby providing just-in-time compilation functionality.

History

The language was created by Yukihiro Matsumoto, who started working on Ruby on February 24, 1993, and released it to the public in 1995. "Ruby" was named as a gemstone because of a joke within Matsumoto's circle of friends alluding to the name of the Perl programming language.
   As of December 2007, the latest stable version of the reference implementation is 1.8.6. Apart from the reference, several other virtual machines are being developed for Ruby. These include JRuby, a port of Ruby to the Java platform, IronRuby, an implementation for the .NET Framework produced by Microsoft, and Rubinius, an interpreter modeled after self-hosting Smalltalk virtual machines.

Philosophy

The language's creator, Yukihiro "Matz" Matsumoto, has said that Ruby is designed for programmer productivity and fun, following the principles of good user interface design. He stresses that systems design needs to emphasize human, rather than computer, needs :
principle of least surprise (POLS), meaning that the language should behave in such a way as to minimize confusion for experienced users. Matsumoto has said his primary design goal was to make a language which he himself enjoyed using, by minimizing programmer work and possible confusion. He has said he hadn't applied the principle of least surprise to the design of Ruby, but nevertheless the phrase has come to be closely associated with the Ruby programming language. The phrase has itself been a source of surprise, as novice users may take it to mean that Ruby's behaviors try to closely match behaviors familiar from other languages. In a May 2005 discussion on the comp.lang.ruby newsgroup, Matsumoto attempted to distance Ruby from POLS, explaining that because any design choice will be surprising to someone, he uses a personal standard in evaluating surprise. If that personal standard remains consistent there will be few surprises for those familiar with the standard.
   Matsumoto defined it this way in an interview:

Semantics

Ruby is object oriented: every data type is an object, including classes and types which many other languages designate as primitives (such as integers, booleans, and "nil"). Every function is a method. Named values (variables) always designate references to objects, not the objects themselves. Ruby supports inheritance with dynamic dispatch, mixins and singleton methods (belonging to, and defined for, a single instance rather than being defined on the class). Though Ruby doesn't support multiple inheritance, classes can import modules as mixins. Procedural syntax is supported, but all methods defined outside of the scope of a particular object are actually methods of the Object class. Since this class is parent to every other class, the changes become visible to all classes and objects.
   Ruby has been described as a multi-paradigm programming language: it allows procedural programming (defining functions/variables outside classes makes them part of the root, 'self' Object), with object orientation (everything is an object) or functional programming (it has anonymous functions, closures, and continuations; statements all have values, and functions return the last evaluation). It has support for introspection, reflection and metaprogramming, as well as support for interpreter-based threads. Ruby features dynamic typing, and supports parametric polymorphism.
   According to the Ruby FAQ, "If you like Perl, you'll like Ruby and be right at home with its syntax. If you like Smalltalk, you'll like Ruby and be right at home with its semantics. If you like Python, you may or may not be put off by the huge difference in design philosophy between Python and Ruby/Perl."

Type system

Ruby checks the type of each expression and sub-expression dynamically, as they're encountered during execution, and will raise a runtime error whenever a type error is found.

Is Ruby type-safe?

There is no universally agreed upon definition of type safe (see TypeSafe at C2.com Wiki), and in the case of Ruby, it depends on which definition one refers to.
   One definition of type-safe language requires that "no operation will be applied to a variable of a wrong type." In this respect, Ruby is probably type safe (given Ruby's semantics, it would be very difficult to prove theoretically, but might be assumed as long as no contradictory code example is found).
Another definition of type-safe program requires that "the program won't have type errors when it runs". In this respect, Ruby is obviously not type safe, since it may by design raise type errors during execution.

Is Ruby strongly typed?

Again, there's no universally agreed upon definition of Strongly Typed. C2.com Wiki lists at least 8 different definitions from different sources.
   According to some of these definitions, Ruby is strongly typed, while according to others it's weakly typed:
  1. Def: A language is strongly typed if type annotations are associated with variable names, rather than with values. If types are attached to values, it's weakly typed. => Ruby is weakly typed.
  2. Def: A language is strongly typed if it contains compile-time checks for type constraint violations. If checking is deferred to run time, it's weakly typed. => Ruby is weakly typed.
  3. Def: A language is strongly typed if there are compile-time or run-time checks for type constraint violations. If no checking is done, it's weakly typed. => Ruby is strongly typed.
  4. Def: A language is strongly typed if conversions between different types are forbidden. If such conversions are allowed, it's weakly typed. => Ruby is weakly typed.
  5. Def: A language is strongly typed if conversions between different types must be indicated explicitly. If implicit conversions are performed, it's weakly typed. => Ruby is weakly typed. (Examples: (2+3.5) (implicit conversion Fixnum to Float) and (if 42 then "a" else "b" end) (implicit conversion from Fixnum to TrueClass))
  6. Def: A language is strongly typed if there's no language-level way to disable or evade the type system. If there are casts or other type-evasive mechanisms, it's weakly typed. => Ruby is assumed to be strongly typed, but very difficult to prove mathematically.
  7. Def: A language is strongly typed if it has a complex, fine-grained type system with compound types. If it has only a few types, or only scalar types, it's weakly typed. => Ruby is strongly typed.
  8. Def: A language is strongly typed if the type of its variables is fixed and doesn't vary over the lifetime of the variable. If the type of the datum stored in a variable can change, the language is weakly typed. => Ruby is weakly typed. (Example: x = 1; x = "s". Type of x changes from Fixnum to String.)

Features

  • object-oriented
  • five levels of variable scope: global, class, instance, local, and block
  • exception handling
  • iterators and closures (based on passing blocks of code)
  • native, Perl-like regular expressions at the language level
  • operator overloading
  • automatic garbage collecting
  • highly portable
  • cooperative multi-threading on all platforms using green threads
  • DLL/shared library dynamic loading on most platforms
  • introspection, reflection and metaprogramming
  • large standard library
  • supports dependency injection
  • supports object runtime alteration
  • continuations and generators (examples in RubyGarden: continuations and generators) Ruby currently lacks full support for Unicode, though it has partial support for UTF-8.

    Interaction

    The Ruby official distribution also includes "irb", an interactive command-line interpreter which can be used to test code quickly. The following code fragment represents a sample session using irb: $ irb irb(main):001:0> puts "Hello, World" Hello, World

    > nil irb(main):002:0> 1+2

    > 3

    Syntax

    The syntax of Ruby is broadly similar to Perl and Python. Class and method definitions are signaled by keywords. In contrast to Perl, variables are not obligatorily prefixed with a sigil. When used, the sigil changes the semantics of scope of the variable. The most striking difference from C and Perl is that keywords are typically used to define logical code blocks, without braces (for example, pair of while (not(expression));" in C/C++/...), actually never runs the statement if the expression is already true.
  • Because constants are references to objects, changing what a constant refers to generates a warning, but modifying the object itself does not. For example, Greeting << " world!" if Greeting == "Hello" doesn't generate an error or warning. This is similar to final variables in Java, but Ruby does also have the functionality to "freeze" an object, unlike Java. Some features which differ notably from other languages:
  • The usual operators for conditional expressions, and and or, don't follow the normal rules of precedence: and doesn't bind tighter than or. Ruby also has expression operators || and && which work as expected.

    Language features

  • Ruby code runs slower than many compiled languages (as is typical for interpreted languages) and other major scripting languages such as Python and Perl. However, in future releases (current revision: 1.9), Ruby will be bytecode compiled to be executed on YARV (Yet Another Ruby VM). Ruby's current memory footprint for the same operations is higher than Perl's and Python's.), Rubinius, Ruby.NET, XRuby and YARV. YARV is Ruby 1.9's official new virtual machine and is no longer a separate project.
       As of Ruby MRI, Ruby is available on a lot of operating systems such as Linux, Mac OS X, Microsoft Windows, Windows CE and most flavors of Unix.

    Criticism

    A number of the design choices made for Ruby have well-known disadvantages:
  • As in BASIC, because variables are not required to be declared before use, typing errors can introduce new variables and cause unexpected behavior.
  • Dynamic typing can cause type errors to be found later in the development process, making them more expensive to fix, and add runtime overhead compared to static typing.
  • The runtime extensible environment enabled by metaprogramming can make programs more difficult to reason about statically and inhibit some types of optimizations.
  • The Ruby threading model uses green threads, and its model has some inherent limitations which render it difficult to use or unsafe in some scenarios.
  • Ruby doesn't yet have native support for Unicode or multibyte strings.
  • Ruby suffers from backward compatibility problems. Ruby 2.0 aims to address all of the aforementioned problems:
  • Native threads will be used instead of green threads.
  • Full support for Unicode strings. Some problems which may not be solved in version 2.0 include:
  • Ruby still lacks a specification, the current C implementation being the de facto reference specification.
  • Although Ruby's most popular framework has some high traffic sites, Ruby on Rails applications have been notoriously difficult to deploy and consume a lot of server resources in handling heavy traffic.

    Repositories and libraries

    The Ruby Application Archive (RAA), as well as RubyForge, serve as repositories for a wide range of Ruby applications and libraries, containing more than seven thousand items. Although the number of applications available doesn't match the volume of material available in the Perl or Python community, there are a wide range of tools and utilities which serve to foster further development in the language. RubyGems has become the standard package manager for Ruby libraries. It is very similar in purpose to Perl's CPAN, although its usage is more like apt-get.

    Further Information

    Get more info on 'Ruby Programming Language'.


    External Link Exchanges

    Do you know how hard it is to get a link from a large encyclopaedia? Well we're different and will prove it. To get a link from us just add the following HTML to your site on a relevant page:

      <a href="http://ruby__programming_language.totallyexplained.com">Ruby (programming language) Totally Explained</a>

    Then simply click through this link from your web page. Our crawlers will verify your link, extract the title of your web page and instantly add a link back to it. If you like you can remove the words Totally Explained and embed the link in article text.
       As long as your link remains in place, we'll keep our link to you right here. Please play fair - our crawlers are watching. Your site must be closely related to this one's topic. Any kind of spamming, dubious practises or removing the link will result in your link from us being dropped and, potentially, your whole site being banned.



  • Copyright © 2007-8 totallyexplained.com | Licensed under the GNU Free Documentation License | Site Map
    This article contains text from the Wikipedia article Ruby (programming language) (History) and is released under the GFDL | RSS Version